package com.istock.state.demo.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.istock.base.state.execute.ExecuteContext;
import com.istock.base.state.execute.StateExecutor;
import com.istock.base.state.model.ExecuteResult;
import com.istock.base.state.model.ExecuteResultEnum;
import com.istock.base.task.model.TaskExecContext;
import com.istock.base.task.model.TaskExecResult;
import com.istock.base.task.service.QiluTaskService;
import com.istock.state.demo.model.OrderGoods;
import com.istock.state.demo.model.OrderInfo;
import com.istock.state.demo.model.OrderPay;
import com.istock.state.demo.service.OrderGoodsService;
import com.istock.state.demo.service.OrderInfoService;
import com.istock.state.demo.service.OrderPayService;

@Repository("OrderTask")
public class OrderTask {

	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private QiluTaskService taskService;
	@Autowired
	private OrderPayService orderPayService;
	@Autowired
	private OrderGoodsService orderGoodsService;
	@Autowired
	private StateExecutor executor;
	
	
	private final String TASK_MEHTOD = "spring:OrderTask.budan";
	private final String ORDER_TYPE_ORDER = "ORDER";
	private final String ORDER_TYPE_PAY = "PAY";
	private final String ORDER_TYPE_GOODS = "GOODS";
	public void scanOrder() {
		//扫描orderInfo表
		List<OrderInfo> orderInfoList = orderInfoService.findExpireOrderInfo();
		for(OrderInfo orderInfo : orderInfoList) {
			try {
				Map<String , String> context = new HashMap<String ,String>();
				context.put("type", ORDER_TYPE_ORDER);
				context.put("id", orderInfo.getId()+"");
				taskService.addQiluTask("ORDER-INFO", orderInfo.getId()+""+orderInfo.getStatus(), TASK_MEHTOD, context, 5, 1, 5);
				taskService.asynExecuteTask("ORDER-INFO", orderInfo.getId()+""+orderInfo.getStatus());
			}catch(Exception e) {
				
			}
		}
		
		List<OrderPay> orderPayList = orderPayService.findExpireOrderPay();
		for(OrderPay orderPay : orderPayList) {
			try {
				Map<String , String> context = new HashMap<String ,String>();
				context.put("type", ORDER_TYPE_PAY);
				context.put("id", orderPay.getId()+"");
				taskService.addQiluTask("ORDER-PAY", orderPay.getId()+""+orderPay.getStatus(), TASK_MEHTOD, context, 5, 1, 5);
				taskService.asynExecuteTask("ORDER-PAY",  orderPay.getId()+""+orderPay.getStatus());
			}catch(Exception e) {}
		}
		
		List<OrderGoods> orderGoodsList = orderGoodsService.findExpireOrderGoods();
		for(OrderGoods orderGoods : orderGoodsList) {
			try {
				Map<String , String> context = new HashMap<String ,String>();
				context.put("type", ORDER_TYPE_GOODS);
				context.put("id", orderGoods.getId()+"");
				taskService.addQiluTask("ORDER-GOODS", orderGoods.getId()+""+orderGoods.getStatus(), TASK_MEHTOD, context, 5, 1, 5);
				taskService.asynExecuteTask("ORDER-GOODS",orderGoods.getId()+""+orderGoods.getStatus());
			}catch(Exception e) {}
		}
	}
	
	public TaskExecResult budan(TaskExecContext context){
		//获得autoTask回调的参数
		Long id = Long.valueOf(context.getContext().get("id"));
		String type = context.getContext().get("type");
		
		//按照类型加载订单
		Object order = null;
		if(type.equalsIgnoreCase(ORDER_TYPE_ORDER)) {
			order = orderInfoService.findOrderInfoById(id);
		}else if(type.equalsIgnoreCase(ORDER_TYPE_PAY)) {
			order = orderPayService.findOrderPayById(id);
		}else if(type.equalsIgnoreCase(ORDER_TYPE_GOODS)) {
			order = orderGoodsService.findOrderGoodsById(id);
		}
		if(order != null) {
			//调用state
			//如果结果不是FAIL,则说明调用成功,当前任务可以关闭了
			ExecuteContext stateContext = new ExecuteContext();
			stateContext.setOrder(order);
			ExecuteResult result = executor.execute(stateContext);
			return new TaskExecResult(result.getResultEnum() == ExecuteResultEnum.SUCCESS || result.getResultEnum() == ExecuteResultEnum.PARTLY_SUCCESS);
		}
		return new TaskExecResult(false);
	}
}
